package com.atguigu.userservice.controller;

import com.atguigu.userservice.service.UcenterMemberService;
import com.atguigu.userservice.utils.WxConstUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * @author Mr.Alan
 * @since 2020-08-25
 */
@Controller
@RequestMapping("/api/ucenter/wx")
@CrossOrigin
public class WxController {

    @Resource
    private UcenterMemberService ucenterMemberService;

    /**
     * 微信扫码登录
     */
    @ApiOperation("/微信登陆")
    @GetMapping("/login")
    public String getCode() {

        String url = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=response_type&scope=snsapi_login";
        url = String.format(url, WxConstUtils.APP_ID, WxConstUtils.REDIRECT_URI);
        return "redirect:" + url;
    }

    /**
     * 微信扫码成功回调方法
     */
    @ApiOperation("/微信扫码成功回调方法")
    @GetMapping("/callback")
    public String callback(String code) {

        String token = ucenterMemberService.loginByWx(code);
        String url = "http://localhost:3000?token=" + token;
        return "redirect:" + url;

    }

}
